2
2
.
.
1
1
.
.
1
1
L
L
i
i
t
t
e
e
r
r
a
a
l
l
s
s
I
I
n
n
f
f
o
o
Literal is string representation of both Data Type and Data Value: 10, "John", 'A'
Literal notations are different ways of constructing literals that represent the same Data Type: 10, +10, 0B1010 or 0xA.
Escape Sequences are used for Character & String Literals to symbolize certain characters that can't be written.
Literal Types
LITERAL
EXAMPLE
NOTATIONS
Null
null
Boolean
true, false
Character
'A'
Integer
65
Decimal: 65 Hexadecimal: 0x41 Binary: 0b100_0001
Long
65L
Decimal: 65L Hexadecimal: 0x41l Binary: 0b100_0001L
Float
65.2F
Basic: 65.2F Scientific: 0.652E2f
Double
65.2
Basic: 65.2 Scientific: 0.652E2
String
"Hello ${name} \n"
N
N
u
u
l
l
l
l
Null Literal represents Null Data Type which can have only one value null.
Null
var age : Int? = null
B
B
o
o
o
o
l
l
e
e
a
a
n
n
Boolean Literal represents Boolean Data Type which can have only two values true or false.
Boolean Literals
var access2 : Boolean = true
var access2 : Boolean = false
I
I
n
n
t
t
e
e
g
g
e
e
r
r
Integer Literal represents signed Integer Data Type and value and supports multiple notations.
Integer Literal Notations
var value : Int = 65 //Decimal notation (base 10).
var value : Int = 0x41 //Hexadecimal notation (base 16).
var value : Int = 0b1000001 //Binary notation (base 2 ).
var value : Int = 10_000 //You can use underscore any way you like: 10_000, 0b10__00_001
L
L
o
o
n
n
g
g
Long Literal represents Long Data Type and value and supports multiple notations.
It is written by adding letter "L" as suffix (in upper or lower case).
Long Literal Notations
var value : Long = 65L //Decimal notation (base 10).
var value : Long = 0x45L //Hexadecimal notation (base 16).
var value : Long = 0b1000001l //Binary notation (base 2 ).
var value : Long = 10_000l //You can use underscore any way you like: 10_000L, 0b10__00_001L
F
F
l
l
o
o
a
a
t
t
[
[
R
R
]
]
Float Literal represents Float Data Type and value and supports multiple notations.
Float literal is used to represent real number.
It is written by adding letter "F" as suffix (in upper or lower case).
Float Literal Notations
var value : Float = 65.23f //Basic notation.
var value : Float = 0.6523E2F //Scientific notation.
D
D
o
o
u
u
b
b
l
l
e
e
[
[
R
R
]
]
Double Literal represents Double Data Type and value and supports multiple notations.
Double Literal Notations
var value : Double = 65.23 //Basic notation.
var value : Double = 0.6523E2 //Scientific notation.
C
C
h
h
a
a
r
r
a
a
c
c
t
t
e
e
r
r
Character literal
represents character data type and value where value is 16-bit Unicode character
is constructed by enclosing character or escape sequence inside single quotes
Escape sequence
can represent any character, including special ones, as shown by the table below
starts with backslash / escape character followed by specific combinations representing a character
Using octal or Unicode character representations is equivalent to typing that character inside your source code.
This differs from escape sequences \n, \' or \" which will not throw error while '\u000D', '\u0027' or '\u0022' might.
Character
//BASIC CHARACTER LITERAL.
var value : Char = 'A' //'A' character.
var value : Char = '\t' //Tab character.
var value : Char = '\"' //Double quote character.
var value : Char = '\'' //Single quote character.
//USING OCTAL ESCAPE SEQUENCE.
var value : Char = '\101' //'A' character.
var value : Char = '\10' //Tab quote character.
var value : Char = '\42' //Double quote character.
var value : Char = '\47' //Single quote character.
//USING UNICODE ESCAPE SEQUENCE. USES HEXADECIMAL ASCII VALUES WITH LEADING ZEROS.
var value : Char = '\u0041' //'A' character.
var value : Char = '\u0009' //Tab character
var value : Char = '\u0022' //Double quote character
var value : Char = '\u0027' //Single quote character reports error.
S
S
t
t
r
r
i
i
n
n
g
g
String literal represents string data type and value.
String literal is used to represent strings.
String literal is written by enclosing sequence of characters inside double quotes.
Using octal or Unicode character representations is equivalent to typing that character inside your source code.
This differs from escape sequences \n, \' or \" which will not throw error while '\u000D', '\u0027' or '\u0022' might.
String
//BASIC STRING LITERAL.
var name : String = "Display letter A"; //"Display letter A" is string literal.
var name : String = "First line \nSecond \t line."; //Escape sequences \n and \t for new line and tab.
//USING OCTAL ESCAPE SEQUENCE.
var name : String = "Display letter \101"; //"Display letter A".
var name : String = "First line \15Second \11 line."; //"First line \nSecond \t line.".
//USING OCTAL ESCAPE SEQUENCE.
var name : String = "Display letter \u0041"; //"Display letter A".
var name : String = "First line \nSecond \u0009 line."; //Replacing \n with unicode causes error.
E
E
s
s
c
c
a
a
p
p
e
e
S
S
e
e
q
q
u
u
e
e
n
n
c
c
e
e
s
s
Escape Sequences are used for Character & String Literals to symbolize certain characters that can't be written.
Escape Sequences
SEQUENCE
DESCRIPTION
\n
Linefeed
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\e
Escape
\f
Form feed
\\
Backslash
\$
Dollar sign
\"
Double quote (only for Double Quotes and Heredoc)
\'
Single quote (only for Single Quotes and Nowdoc)
\d
Octal representation of character as defined in ASCII Table (For 'A' it is '\101' ).
\ud
Unicode representation of character as defined in ASCII Table (For 'A' it is '\u0041').